home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / pvrgjpeg / marker.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  18KB  |  806 lines

  1. /*************************************************************
  2. Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
  3. PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
  4. Group. If you use this software, you agree to the following: This
  5. program package is purely experimental, and is licensed "as is".
  6. Permission is granted to use, modify, and distribute this program
  7. without charge for any purpose, provided this license/ disclaimer
  8. notice appears in the copies.  No warranty or maintenance is given,
  9. either expressed or implied.  In no event shall the author(s) be
  10. liable to you or a third party for any special, incidental,
  11. consequential, or other damages, arising out of the use or inability
  12. to use the program for any purpose (or the loss of data), even if we
  13. have been advised of such possibilities.  Any public reference or
  14. advertisement of this source code should refer to it as the Portable
  15. Video Research Group (PVRG) code, and not by any author(s) (or
  16. Stanford University) name.
  17. *************************************************************/
  18.  
  19. /*
  20. ************************************************************
  21. marker.c
  22.  
  23. This file contains the Marker library which uses the direct buffer
  24. access routines bgetc...
  25.  
  26. ************************************************************
  27. */
  28.  
  29. /*LABEL marker.c */
  30.  
  31. /*Include files */
  32. #include "globals.h"
  33. #include "stream.h"
  34. #include "marker.h"
  35. #ifdef SYSV
  36. #include <sys/fcntl.h>
  37. #endif
  38.  
  39. /*PUBLIC*/
  40. extern void WriteSoi();
  41. extern void WriteEoi();
  42. extern void WriteJfif();
  43. extern void WriteSof();
  44. extern void WriteDri();
  45. extern void WriteDqt();
  46. extern void WriteSos();
  47. extern void WriteDht();
  48. extern void ReadSof();
  49. extern void ReadDqt();
  50. extern void ReadDht();
  51. extern void ReadDri();
  52. extern void ReadDnl();
  53. extern int CheckMarker();
  54. extern void CheckScan();
  55. extern void ReadSos();
  56. extern void MakeConsistentFrameSize();
  57. /*PRIVATE*/
  58.  
  59. /* External marker definition */
  60.  
  61. extern FRAME *CFrame;
  62. extern IMAGE *CImage;
  63. extern SCAN *CScan;
  64. extern int NumberMDU;
  65. extern int Loud;
  66. extern int izigzag_index[];
  67.  
  68. #define Zigzag(i) izigzag_index[i]
  69.  
  70. /*START*/
  71.  
  72. /*BFUNC
  73.  
  74. WriteSoi() puts an SOI marker onto the stream.
  75.  
  76. EFUNC*/
  77.  
  78. void WriteSoi()
  79. {
  80.   BEGIN("WriteSoi");
  81.  
  82.   swbytealign();
  83.   bputc(MARKER_MARKER);
  84.   bputc(MARKER_SOI);
  85. }
  86.  
  87. /*BFUNC
  88.  
  89. WriteEoi() puts an EOI marker onto the stream.
  90.  
  91. EFUNC*/
  92.  
  93. void WriteEoi()
  94. {
  95.   BEGIN("WriteEoi");
  96.  
  97.   swbytealign();
  98.   bputc(MARKER_MARKER);
  99.   bputc(MARKER_EOI);
  100. }
  101.  
  102. /*BFUNC
  103.  
  104. WriteJfif() puts an JFIF APP0 marker onto the stream.  This is a
  105. generic 1x1 aspect ratio, no thumbnail specification.
  106.  
  107. EFUNC*/
  108.  
  109. void WriteJfif()
  110. {
  111.   BEGIN("WriteJfif");
  112.   int Start,End;
  113.  
  114.   swbytealign();
  115.   bputc(MARKER_MARKER);
  116.   bputc(MARKER_APP);
  117.   Start = swtell();              /* Find out the start of position. */
  118.   bputw(0);                      /* Put a 0 down onto the stream. */
  119.   bputc(0x4a); bputc(0x46); bputc(0x49); bputc(0x46); bputc(0x00);
  120.   bputc(0x01); bputc(0x02);   /*Version 1.02*/
  121.   bputc(0x00); /* No absolute DPI */
  122.   bputw(1);    /* Aspect ratio */
  123.   bputw(1);
  124.   bputc(0x00); /* No thumbnails */
  125.   bputc(0x00);
  126.  
  127.   End = swtell();                /* Find out end of the marker. */
  128.   swseek(Start);                 /* Rewind */
  129.   bputw((End-Start) >> 3);       /* Put marker there. */
  130.   if ((Start-End) & 0x7)         /* if not byte flush, then problems. */
  131.     {
  132.       WHEREAMI();
  133.       printf("Bad frame marker, not byte flush.\n");
  134.     }
  135.   swseek(End);
  136. }
  137. /*BFUNC
  138.  
  139. WriteSof() puts an SOF marker onto the stream.
  140.  
  141. EFUNC*/
  142.  
  143. void WriteSof()
  144. {
  145.   BEGIN("WriteSof");
  146.   int i,j,Start,End;
  147.  
  148.   swbytealign();
  149.   bputc(MARKER_MARKER);
  150.   bputc(MARKER_SOF|(CFrame->Type&0xf));
  151.   Start = swtell();              /* Find out the start of position. */
  152.   bputw(0);                      /* Put a 0 down onto the stream. */
  153.   bputc(CFrame->DataPrecision);
  154.   if (!CFrame->InsertDnl) {bputw(CFrame->GlobalHeight);}
  155.   else {bputw(0);}
  156.   bputw(CFrame->GlobalWidth);
  157.   bputc(CFrame->GlobalNumberComponents);
  158.   for(i=0;i<CFrame->GlobalNumberComponents;i++)
  159.     {
  160.       bputc(j=CFrame->cn[i]);               /* Store off in index */
  161.       bputn(CFrame->hf[j],CFrame->vf[j]);
  162.       bputc(CFrame->tq[j]);
  163.     }
  164.   End = swtell();                /* Find out end of the marker. */
  165.   swseek(Start);                 /* Rewind */
  166.   bputw((End-Start) >> 3);       /* Put marker there. */
  167.   if ((Start-End) & 0x7)         /* if not byte flush, then problems. */
  168.     {
  169.       WHEREAMI();
  170.       printf("Bad frame marker, not byte flush.\n");
  171.     }
  172.   swseek(End);
  173. }
  174.  
  175. /*BFUNC
  176.  
  177. WriteDri() writes out a resync (or restart) interval out to the
  178. stream. If unspecified, resync is not enabled.
  179.  
  180. EFUNC*/
  181.  
  182. void WriteDri()
  183. {
  184.   BEGIN("WriteDri");
  185.  
  186.   swbytealign();
  187.   bputc(MARKER_MARKER);
  188.   bputc(MARKER_DRI);
  189.   bputw(4); /* Constant length of 4 */
  190.   bputw(CFrame->ResyncInterval);
  191. }
  192.  
  193. /*BFUNC
  194.  
  195. WriteDnl() writes out a number of line marker out to the stream.  Note
  196. that we must have defined number of lines before as 0.
  197.  
  198. EFUNC*/
  199.  
  200.  
  201. WriteDnl()
  202. {
  203.   BEGIN("WriteDnl");
  204.  
  205.   swbytealign();
  206.   bputc(MARKER_MARKER);
  207.   bputc(MARKER_DNL);
  208.   bputw(4); /* Constant length of 4 */
  209.   bputw(CFrame->GlobalHeight);
  210. }
  211.  
  212. /*BFUNC
  213.  
  214. WriteDqt() writes out the quantization matrices in the CImage
  215. structure.
  216.  
  217. EFUNC*/
  218.  
  219. void WriteDqt()
  220. {
  221.   BEGIN("WriteDqt");
  222.   int i,j,bignum_p,Start,End,*qmatrix;
  223.  
  224.   if (!(CScan->NumberQTablesSend))
  225.     return;   /* No tables to transmit, then ignore. */
  226.   swbytealign();
  227.   bputc(MARKER_MARKER);
  228.   bputc(MARKER_DQT);
  229.   Start = swtell();
  230.   bputw(0);
  231.   for(i=0;i<CScan->NumberQTablesSend;i++)
  232.     {
  233.       qmatrix = CImage->QuantizationMatrices[CScan->sq[i]];
  234.       for(bignum_p=0,j=63;j>=0;j--)
  235.     {
  236.       if(qmatrix[j]>255)
  237.         {
  238.           bignum_p=0x10;
  239.           break;
  240.         }
  241.     }
  242.       bputc((bignum_p|CScan->sq[i])); /* Precision defined for big numbers */
  243.       if (bignum_p)
  244.     {
  245.       for(j=0;j<64;j++)
  246.         bputw(qmatrix[Zigzag(j)]);
  247.     }
  248.       else
  249.     {
  250.       for(j=0;j<64;j++)
  251.         bputc(qmatrix[Zigzag(j)]);
  252.     }
  253.     }
  254.   CScan->NumberQTablesSend=0; /* Clear out queue */
  255.   End = swtell();       /* Assume a marker code will follow.*/
  256.   swseek(Start);        /* bputc(END_QUANTIZATION_TABLE);*/
  257.   bputw((End-Start) >> 3);
  258.   if ((Start-End) & 0x7)
  259.     {
  260.       WHEREAMI();
  261.       printf("DQT marker not byte flush.\n");
  262.     }
  263.   swseek(End);
  264. }
  265.  
  266. /*BFUNC
  267.  
  268. WriteSos() writes a start of scan marker.
  269.  
  270. EFUNC*/
  271.  
  272. void WriteSos()
  273. {
  274.   BEGIN("WriteSos");
  275.   int i,Start,End;
  276.  
  277.   swbytealign();
  278.   bputc(MARKER_MARKER);
  279.   bputc(MARKER_SOS);
  280.   Start = swtell();
  281.   bputw(0);
  282.   bputc(CScan->NumberComponents);
  283.   for(i=0;i<CScan->NumberComponents;i++)
  284.     {
  285.       bputc(CScan->ci[i]);
  286.       bputn(CScan->td[i],CScan->ta[i]);
  287.     }
  288.   bputc(CScan->SSS);
  289.   bputc(CScan->SSE);
  290.   bputn(CScan->SAH,CScan->SAL);
  291.   End = swtell();
  292.   swseek(Start);
  293.   bputw((End-Start) >> 3);
  294.   if ((Start-End) & 0x7)
  295.     {
  296.       WHEREAMI();
  297.       printf("Bad scan marker not byte flush.\n");
  298.     }
  299.   swseek(End);
  300. }
  301.  
  302. /*BFUNC
  303.  
  304. WriteDht() writes out the Huffman tables to send.
  305.  
  306. EFUNC*/
  307.  
  308. void WriteDht()
  309. {
  310.   BEGIN("WriteDht");
  311.   int i,Start,End;
  312.  
  313.   if (!(CScan->NumberDCTablesSend) && !(CScan->NumberACTablesSend))
  314.     return;   /* No tables to transmit, then ignore. */
  315.   swbytealign();
  316.   bputc(MARKER_MARKER);
  317.   bputc(MARKER_DHT);
  318.   Start = swtell();
  319.   bputw(0);
  320.   for(i=0;i<CScan->NumberDCTablesSend;i++)
  321.     {
  322.       bputc(CScan->sd[i]);
  323.       UseDCHuffman(CScan->sd[i]);
  324.       WriteHuffman();
  325.     }
  326.   for(i=0;i<CScan->NumberACTablesSend;i++)
  327.     {
  328.       bputc(CScan->sa[i]|0x10);
  329.       UseACHuffman(CScan->sa[i]);
  330.       WriteHuffman();
  331.     }
  332.   CScan->NumberDCTablesSend=0; /* Clear out send queue */
  333.   CScan->NumberACTablesSend=0;
  334.   /*
  335.     We end on a new marker... so an end of code table is unnecessary.
  336.     bputc(END_CODE_TABLE);
  337.     */
  338.   End = swtell();
  339.   swseek(Start);
  340.   bputw((End-Start) >> 3);
  341.   if ((Start-End) & 0x7)
  342.     {
  343.       WHEREAMI();
  344.       printf("Bad scan marker not byte flush.\n");
  345.     }
  346.   swseek(End);
  347. }
  348.  
  349.  
  350. /*BFUNC
  351.   
  352.   ReadSof() reads a start of frame marker from the stream. We assume that
  353.   the first two bytes (marker prefix) have already been stripped.
  354.   
  355.   EFUNC*/
  356.  
  357. void ReadSof(Type)
  358.      int Type;
  359. {
  360.   BEGIN("ReadSof");
  361.   int i,j,Length,Start,End,rb;
  362.   
  363.   Start = srtell();
  364.   Length = bgetw();
  365.   if (Loud > MUTE)
  366.     printf("Frame Length %d\n",Length);
  367.   CFrame->Type=Type;
  368.   CFrame->DataPrecision = bgetc();
  369.   CFrame->GlobalHeight = bgetw();
  370.   CFrame->GlobalWidth = bgetw();
  371.   
  372.   for(i=0;i<MAXIMUM_COMPONENTS;i++)
  373.     CFrame->hf[i]=CFrame->vf[i]=CFrame->tq[i]=0;
  374.   CFrame->GlobalNumberComponents = bgetc();
  375.   for(i=0;i<CFrame->GlobalNumberComponents;i++)
  376.     {
  377.       j = bgetc();
  378.       rb = bgetc();
  379.       CFrame->cn[i] = j;
  380.       CFrame->hf[j] = hinyb(rb);
  381.       CFrame->vf[j] = lonyb(rb);
  382.       CFrame->tq[j] = bgetc();
  383.     }
  384.   MakeConsistentFrameSize();
  385.   End = srtell();
  386.   if ((End-Start) != (Length<<3))
  387.     {
  388.       WHEREAMI();
  389.       printf("Bad read frame length.\n");
  390.     }
  391.   if (Loud > MUTE)
  392.     {
  393.       PrintImage();
  394.       PrintFrame();
  395.     }
  396. }
  397.  
  398. /*BFUNC
  399.   
  400.   ReadDqt() reads a quantization table marker from the stream.
  401.   The first two bytes have been stripped off.
  402.   
  403.   EFUNC*/
  404.  
  405. void ReadDqt()
  406. {
  407.   BEGIN("ReadDqt");
  408.   int i,Length,Qget,Index,Precision,Start,End;
  409.   
  410.   Start = srtell();
  411.   Length = bgetw();
  412.   if (Loud > MUTE)
  413.     printf("Quantization Length %d\n",Length);
  414.   while((Qget=bgetc()) != END_QUANTIZATION_TABLE)
  415.     {
  416.       Index = Qget & 0xf;
  417.       Precision = (Qget >> 4)&0xf;
  418.       if (Precision > 1)
  419.     {
  420.       printf("Bad Precision: %d  in Quantization Download\n",
  421.          Precision);
  422.       printf("*** Dumping Image ***\n");
  423.       PrintImage();
  424.       printf("*** Dumping Frame ***\n");
  425.       PrintFrame();
  426.       exit(ERROR_MARKER);
  427.     }                             /* Load in q-matrices */
  428.       CImage->QuantizationMatrices[Index] = (int *) calloc(65,sizeof(int));
  429.       if (Precision)               /* If precision then word quantization*/
  430.     {
  431.       for(i=0;i<64;i++)
  432.         {
  433.           if (!(CImage->QuantizationMatrices[Index][Zigzag(i)]=bgetw()))
  434.         {
  435.           printf("marker.c:ReadDqt: Quantization value of zero.\n");
  436.           if (i)
  437.             {
  438.               printf("marker.c:ReadDqt: Changing to i-1.\n");
  439.               CImage->QuantizationMatrices[Index][Zigzag(i)]=
  440.             CImage->QuantizationMatrices[Index][Zigzag(i-1)];
  441.             }
  442.           else
  443.             {            
  444.               printf("marker.c:ReadDqt: Changing to 16.\n");
  445.               CImage->QuantizationMatrices[Index][Zigzag(i)]=16;
  446.             }
  447.         }
  448.         }
  449.     }
  450.       else                       /* Otherwise byte quantization */
  451.     {
  452.       for(i=0;i<64;i++)
  453.         {
  454.           if (!(CImage->QuantizationMatrices[Index][Zigzag(i)]=bgetc()))
  455.         {
  456.           printf("marker.c:ReadDqt: Quantization value of zero.\n");
  457.           if (i)
  458.             {
  459.               printf("marker.c:ReadDqt: Changing to i-1.\n");
  460.               CImage->QuantizationMatrices[Index][Zigzag(i)]=
  461.             CImage->QuantizationMatrices[Index][Zigzag(i-1)];
  462.             }
  463.           else
  464.             {            
  465.               printf("marker.c:ReadDqt: Changing to 16.\n");
  466.               CImage->QuantizationMatrices[Index][Zigzag(i)]=16;
  467.             }
  468.         }
  469.         }
  470.     }
  471.     }
  472.   bpushc(END_QUANTIZATION_TABLE);
  473.   End = srtell();
  474.   if ((End-Start) != (Length<<3))
  475.     {
  476.       WHEREAMI();
  477.       printf("Bad DQT read length.\n");
  478.     }
  479.   if (Loud > MUTE)
  480.     {
  481.       PrintImage();
  482.       PrintFrame();
  483.     }
  484. }
  485.  
  486.  
  487. /*BFUNC
  488.   
  489.   ReadDht() reads a Huffman marker from the stream. We assume that the
  490.   first two bytes have been stripped off.
  491.   
  492.   EFUNC*/
  493.  
  494. void ReadDht()
  495. {
  496.   BEGIN("ReadDht");
  497.   int Index,Where,Length,Start,End;
  498.   
  499.   Start = srtell();
  500.   Length = bgetw();
  501.   if (Loud > MUTE)
  502.     printf("Define Huffman length %d\n",Length);
  503.   while((Index = bgetc()) != END_CODE_TABLE)
  504.     {
  505.       Where = (Index >> 4) & 0x0f;       /* Find location to place it in */
  506.       Index = Index & 0x0f;
  507.       MakeXhuff();                       /* Make Huffman table */
  508.       MakeDhuff();
  509.       ReadHuffman();
  510.       if (Where)
  511.     {
  512.       SetACHuffman(Index);           /* Set current Huffman limit */
  513.       CImage->NumberACTables = MAX(CImage->NumberACTables,(Index+1));
  514.     }
  515.       else
  516.     {
  517.       SetDCHuffman(Index);
  518.       CImage->NumberDCTables = MAX(CImage->NumberDCTables,(Index+1));
  519.     }
  520.     }
  521.   bpushc(END_CODE_TABLE);
  522.   End = srtell();
  523.   if ((End-Start) != (Length<<3))
  524.     {
  525.       WHEREAMI();
  526.       printf("Bad DHT length.\n");
  527.     }
  528.   if (Loud > MUTE)
  529.     PrintImage();
  530. }
  531.  
  532. /*BFUNC
  533.   
  534.   ReadDri() reads a resync interval marker from the stream. We assume
  535.   the first two bytes are stripped off.
  536.   
  537.   EFUNC*/
  538.  
  539. void ReadDri()
  540. {
  541.   BEGIN("ReadDri");
  542.   int Length;
  543.   
  544.   if ((Length=bgetw())!=4)            /* Constant length of 4 */
  545.     {
  546.       WHEREAMI();
  547.       printf("Bad length %d, should be 4.\n",Length);
  548.     }
  549.   CFrame->ResyncInterval = bgetw();
  550. }
  551.  
  552. /*BFUNC
  553.   
  554.   ReadDnl() reads a number of lines marker from the stream. The first
  555.   two bytes should be stripped off.
  556.   
  557.   EFUNC*/
  558.  
  559. void ReadDnl()
  560. {
  561.   BEGIN("ReadDnl");
  562.   int Length;
  563.   
  564.   if ((Length=bgetw())!=4)             /* Constant length of 4 */
  565.     printf("marker.c:ReadDnl: Bad length %d, should be 4.\n",Length);
  566.   CFrame->GlobalHeight = bgetw();
  567.   if (CScan->NumberComponents)
  568.     {
  569.       MakeConsistentFrameSize();
  570.       CheckScan();
  571.       ResizeIob();
  572.       if (CFrame->GlobalHeight)
  573.     {
  574.       InstallIob(0);
  575.       if (CFrame->Type==3)
  576.         NumberMDU = CScan->MDUWide*CScan->MDUHigh;
  577.       else
  578.         NumberMDU = CScan->MDUWide*CScan->MDUHigh;
  579.     }
  580.       else
  581.     NumberMDU = -1;
  582.     }
  583. }
  584.  
  585. /*BFUNC
  586.  
  587. CheckMarker() checks to see if there is a marker in the stream ahead.
  588. This function presumes that ungetc is not allowed to push more than
  589. one byte back.
  590.  
  591. EFUNC*/
  592.  
  593. int CheckMarker()
  594. {
  595.   BEGIN("CheckMarker");
  596.   int Length;
  597.   int v1;
  598.  
  599.   Length = brtell();
  600.   v1=bgetw();
  601.  
  602.   if (v1>=0xffc0)
  603.     {
  604.       brseek(Length,0L);
  605.       return(v1&0xff);
  606.     }
  607.   brseek(Length,0L);
  608.   return(0);
  609. }
  610.  
  611. /*BFUNC
  612.  
  613. ReadSos() reads in a start of scan from the stream. The first two
  614. bytes should have been stripped off.
  615.  
  616. EFUNC*/
  617.  
  618. void ReadSos()
  619. {
  620.   BEGIN("ReadSos");
  621.   int i,Length,Start,End,rb;
  622.   
  623.   Start = srtell();
  624.   Length = bgetw();
  625.   if (Loud > MUTE)
  626.     {
  627.       WHEREAMI();
  628.       printf("Scan length %d\n",Length);
  629.     }
  630.   CScan->NumberComponents = bgetc();
  631.   for(i=0;i<CScan->NumberComponents;i++)
  632.     {
  633.       CScan->ci[i] = bgetc();
  634.       rb = bgetc();
  635.       CScan->td[i] =  hinyb(rb);
  636.       CScan->ta[i] = lonyb(rb);
  637.     }
  638.   CScan->SSS = bgetc();
  639.   CScan->SSE = bgetc();
  640.   rb = bgetc();
  641.   CScan->SAH = hinyb(rb);
  642.   CScan->SAL = lonyb(rb);
  643.   
  644.   End = srtell();
  645.   if ((End-Start) != (Length<<3))
  646.     {
  647.       WHEREAMI();
  648.       printf("Bad scan length.\n");
  649.     }
  650.   if (Loud > MUTE)
  651.     PrintScan();
  652.   MakeConsistentFileNames();   /* A Scan marker always makes new files */
  653.   CheckValidity();
  654.   CheckBaseline();
  655.   CheckScan();
  656.   /* Create the io buffer structure */
  657.  
  658.   if (CFrame->Type==3)
  659.     {
  660.       MakeIob(IOB_LINE,O_RDWR | O_CREAT,
  661.           ((CFrame->DataPrecision>8)?2:1));
  662.       if (CFrame->GlobalHeight)
  663.     {
  664.       InstallIob(0);
  665.       NumberMDU = CScan->MDUWide*CScan->MDUHigh;
  666.     }
  667.       else NumberMDU = -1;
  668.     }
  669.   else
  670.     {
  671.       MakeIob(IOB_BLOCK,O_WRONLY | O_CREAT | O_TRUNC,
  672.           ((CFrame->DataPrecision>8)?2:1));
  673.       if (CFrame->GlobalHeight)
  674.     {
  675.       InstallIob(0);
  676.       NumberMDU = CScan->MDUWide*CScan->MDUHigh;
  677.     }
  678.       else NumberMDU = -1;
  679.     }
  680.  
  681.   /* Sometimes rewinding is necessary */
  682.   /* for(i=0;i<CScan->NumberComponents;i++)
  683.     {
  684.       InstallIob(i);
  685.       RewindIob();
  686.     } */
  687.   ResetCodec();                /* Reset codec for information */
  688. }
  689.  
  690. /*BFUNC
  691.  
  692. CheckScan() sets the MDU dimensions for the CScan structure.
  693.  
  694. EFUNC*/
  695.  
  696. void CheckScan()
  697. {
  698.   int i;
  699.  
  700.   if (CScan->NumberComponents==1)
  701.     {
  702.       i = (((CFrame->GlobalWidth*CFrame->hf[CScan->ci[0]])-1)/CFrame->Maxh)+1;
  703.       if (CFrame->Type!=3)
  704.     i = ((i-1)/8)+1;
  705.       CScan->MDUWide = i;
  706.  
  707.       i = (((CFrame->GlobalHeight*CFrame->vf[CScan->ci[0]])-1)/CFrame->Maxv)+1;
  708.       if (CFrame->Type!=3)
  709.     i = ((i-1)/8)+1;
  710.       CScan->MDUHigh = i;
  711.     }
  712.   else
  713.     {
  714.       CScan->MDUWide=CFrame->MDUWide;
  715.       CScan->MDUHigh=CFrame->MDUHigh;
  716.     }
  717. }
  718.  
  719. /*BFUNC
  720.  
  721. MakeConsistentFrameSize() makes a consistent frame size for all of the
  722. horizontal and vertical frequencies read.
  723.  
  724. EFUNC*/
  725.  
  726. void MakeConsistentFrameSize()
  727. {
  728.   BEGIN("MakeConsistentFrameSize");
  729.   int i,Maxh,Maxv;
  730.   int TestWide, TestHigh;
  731.  
  732.   Maxv = Maxh = 1;
  733.   for(i=0;i<MAXIMUM_COMPONENTS;i++)
  734.     {
  735.       if (CFrame->vf[i] > Maxv)
  736.     Maxv = CFrame->vf[i];
  737.       if (CFrame->hf[i] > Maxh)
  738.     Maxh = CFrame->hf[i];
  739.     }
  740.  
  741.   for(i=0;i<MAXIMUM_COMPONENTS;i++)       /* Define estimated actual width */
  742.     {                                     /* ignoring replications */
  743.       if (CFrame->hf[i])
  744.     {
  745.       if (!CFrame->Width[i])
  746.         CFrame->Width[i] =
  747.           (((CFrame->GlobalWidth*CFrame->hf[i])-1)/Maxh)+1;
  748.       if (!CFrame->Height[i])
  749.         CFrame->Height[i] = 
  750.           (((CFrame->GlobalHeight*CFrame->vf[i])-1)/Maxv)+1;
  751.     }
  752.     }
  753.  
  754.   CFrame->Maxv = Maxv;  CFrame->Maxh = Maxh;
  755.  
  756.   CFrame->MDUWide = (CFrame->GlobalWidth-1)/Maxh +1;
  757.   if (CFrame->GlobalHeight)
  758.     CFrame->MDUHigh = (CFrame->GlobalHeight-1)/Maxv +1;
  759.   else
  760.     CFrame->MDUHigh = 0;
  761.  
  762.   if (CFrame->Type!=3)
  763.     {
  764.       CFrame->MDUWide= (CFrame->MDUWide-1)/8 +1;
  765.       if (CFrame->MDUHigh)
  766.     CFrame->MDUHigh= (CFrame->MDUHigh-1)/8 +1;
  767.     }
  768.  
  769.   for(i=0;i<MAXIMUM_COMPONENTS;i++)
  770.     {
  771.       if (CFrame->hf[i])
  772.     {
  773.       TestWide = ((CFrame->Width[i]-1)/(CFrame->hf[i]))+1;
  774.       if (CFrame->Type!=3) TestWide= (TestWide-1)/8 +1;
  775.  
  776.       if (CFrame->MDUWide!=TestWide)
  777.         {
  778.           WHEREAMI();
  779.           printf("Inconsistent frame width.\n");
  780.           printf("Component[%dx%d]\n",
  781.              CFrame->Width[i],CFrame->Height[i]);
  782.           printf("MDU Wide: Image, Component %d!= %d.\n",
  783.              CFrame->MDUWide,TestWide);
  784.         }
  785.       if (CFrame->MDUHigh)
  786.         {
  787.           TestHigh = ((CFrame->Height[i]-1)/(CFrame->vf[i]))+1;
  788.           if (CFrame->Type!=3) TestHigh= (TestHigh-1)/8 +1;
  789.           if (CFrame->MDUHigh!=TestHigh)
  790.         {
  791.           WHEREAMI();
  792.           printf("Inconsistent frame height.\n");
  793.           printf("Component[%dx%d]\n",
  794.              CFrame->Width[i],CFrame->Height[i]);
  795.           printf("MDU High: Image, Component %d!= %d.\n",
  796.              CFrame->MDUHigh,TestHigh);
  797.         }
  798.         }
  799.     }
  800.     }
  801. }
  802.  
  803.  
  804.  
  805. /*END*/
  806.